//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package org.tuiofx.internal.base;

import java.util.HashMap;
import java.util.Map;

import javafx.event.EventTarget;
import javafx.scene.Node;
import javafx.scene.Scene;
import org.tuiofx.Configuration;
import org.tuiofx.internal.gesture.TuioTouchPoint;
import org.tuiofx.internal.util.LogUtils;

public class TouchHandler {
    private final GestureHandler gestureHandler;
    private final Configuration config;
    private final TargetSelection targetSelection;
    Map<Integer, TuioTouchPoint> touchPoints = null;
    private final CoordinatesMapping coordinatesMapping;

    public TouchHandler(GestureHandler gestureHandler, TargetSelection targetSelection, CoordinatesMapping coordinatesMapping, Configuration config) {
        this.gestureHandler = gestureHandler;
        this.touchPoints = new HashMap<>();
        this.config = config;
        this.targetSelection = targetSelection;
        this.coordinatesMapping = coordinatesMapping;
    }

    public void touchesBegan(TuioTouchPoint touchPoint) {
        if (this.touchPoints.containsKey(touchPoint.getSessionID())) {
            LogUtils.warn(" ADD TOUCH ID " + touchPoint.getSessionID() + " FAILED --> already exists");
        } else {
            this.touchPoints.put(touchPoint.getSessionID(), touchPoint);
            EventTarget targetNode = this.targetSelection.selectTargetNode(touchPoint);
            if (targetNode != null) {
                touchPoint.setTargetNode(targetNode);
                touchPoint.setCurrentTargetNode(targetNode);
                Scene targetScene = ((Node) targetNode).getScene();
                if (this.targetSelection.isNodeInTheCurrentScene((Node) targetNode)) {
                    FXCoordinates fxCords = this.coordinatesMapping.translateFXCoordinatesToScene(touchPoint.getScreenX(), touchPoint.getScreenY(), targetScene);
                    touchPoint.setFXCoordinates(fxCords);
                }

                this.gestureHandler.touchesBegan(touchPoint);
            }
        }
    }

    public void touchesMove(TuioTouchPoint touchPoint) {
        TuioTouchPoint oldTouchPoint = (TuioTouchPoint) this.touchPoints.get(touchPoint.getSessionID());
        if (oldTouchPoint == null) {
            LogUtils.warn("MOVE TOUCH ID " + touchPoint.getSessionID() + " FAILED --> not found");
        } else if (oldTouchPoint.getTargetNode() != null) {
            touchPoint.update(oldTouchPoint.getTargetNode(), oldTouchPoint.getSceneX(), oldTouchPoint.getSceneY());
            EventTarget targetNode = touchPoint.getTargetNode();
            EventTarget newTargetNode = this.targetSelection.selectTargetNode(touchPoint);
            touchPoint.setCurrentTargetNode(newTargetNode);
            Scene targetScene = ((Node) targetNode).getScene();
            if (targetScene == null) {
                System.out.println("NO targetScene");
            } else {
                if (this.targetSelection.isNodeInTheCurrentScene((Node) targetNode)) {
                    FXCoordinates fxCords = this.coordinatesMapping.translateFXCoordinatesToScene(touchPoint.getScreenX(), touchPoint.getScreenY(), targetScene);
                    touchPoint.setFXCoordinates(fxCords);
                }

                this.gestureHandler.touchesMove(touchPoint);
            }
        }
    }

    public void touchesEnd(TuioTouchPoint touchPoint) {
        TuioTouchPoint oldTouchPoint = (TuioTouchPoint) this.touchPoints.get(touchPoint.getSessionID());
        if (oldTouchPoint == null) {
            LogUtils.error("++++++ oldTouchPoint should not be null");
        } else {
            touchPoint.update(oldTouchPoint.getTargetNode(), oldTouchPoint.getSceneX(), oldTouchPoint.getSceneY());
            Node targetNode = (Node) touchPoint.getTargetNode();
            if (targetNode != null) {
                Scene targetScene = targetNode.getScene();
                if (targetScene == null) {
                    System.out.println("NO targetScene");
                    return;
                }

                if (this.targetSelection.isNodeInTheCurrentScene(targetNode)) {
                    FXCoordinates fxCords = this.coordinatesMapping.translateFXCoordinatesToScene(touchPoint.getScreenX(), touchPoint.getScreenY(), targetScene);
                    touchPoint.setFXCoordinates(fxCords);
                }
            }

            EventTarget newTargetNode = this.targetSelection.selectTargetNode(touchPoint);
            touchPoint.setCurrentTargetNode(newTargetNode);
            this.touchPoints.remove(touchPoint.getSessionID());
            this.gestureHandler.touchesEnd(touchPoint);
        }
    }
}
